package com.huqing.icu.utils;

import com.huqing.icu.protocol.SystemResponseCode;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 响应帮助类
 */
public class ProtocolUtils {

    /**
     * 组装json响应格式-格式1
     *
     * @param code
     * @param message
     * @param data
     * @return
     */
    public static Map<String, Object> populateSimpleResponseJson(int code, String message, Object data) {
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put("code", code);
        jsonMap.put("msg", StringUtils.isBlank(message) ? "Ok" : message);
        jsonMap.put("data", data);
        return jsonMap;
    }

    /**
     * 组装json响应格式-格式1
     *
     * @param data
     * @return
     */
    public static String populateResponseJson(Object data) {
        return populateResponseJson(SystemResponseCode.OK.getCode(), SystemResponseCode.OK.getMsg(), data);
    }

    /**
     * 组装json响应格式-格式2
     *
     * @param msg
     * @param data
     * @return
     */
    public static String populateResponseJson(String message, Object data) {
        if (StringUtils.isBlank(message))
            message = SystemResponseCode.OK.getMsg();
        return populateResponseJson(SystemResponseCode.OK.getCode(), message, data);
    }

    /**
     * 组装json响应格式-格式2
     *
     * @param message
     * @param data
     * @return
     */
    public static String populateResponseJsonForNull(String message, Object data) {
        if (StringUtils.isBlank(message))
            message = SystemResponseCode.OK.getMsg();
        return populateResponseJsonForNull(SystemResponseCode.OK.getCode(), message, data);
    }

    public static String populateResponseJsonForNull(int code, String message, Object data) {
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        Map<String, Object> stateMap = new HashMap<String, Object>();
        stateMap.put("code", code);
        stateMap.put("msg", StringUtils.isBlank(message) ? SystemResponseCode.OK.getMsg() : message);
        jsonMap.put("state", stateMap);
        jsonMap.put("data", data);
        return JsonUtils.obj2StringForNull(jsonMap);
    }

    /**
     * 组装json响应格式-格式3
     *
     * @param code
     * @param message
     * @param data
     * @return
     */
    private static String populateResponseJson(int code, String message, Object data) {
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        Map<String, Object> stateMap = new HashMap<String, Object>();
        stateMap.put("code", code);
        stateMap.put("msg", StringUtils.isBlank(message) ? SystemResponseCode.OK.getMsg() : message);
        jsonMap.put("state", stateMap);
        jsonMap.put("data", data);
        return JsonUtils.obj2String(jsonMap);
    }

    /**
     * 组装json响应格式-错误1
     *
     * @param code
     * @return
     */
    public static String populateResponseJson() {
        return populateResponseJson(SystemResponseCode.INTERNAL_ERROR.getCode(), SystemResponseCode.INTERNAL_ERROR.getMsg());
    }

    /**
     * 组装json响应格式-错误2
     *
     * @param code
     * @return
     */
    public static String populateResponseJson(int code) {
        SystemResponseCode[] systemResponseCodes = SystemResponseCode.values();
        for (SystemResponseCode systemResponseCode : systemResponseCodes) {
            if (code == systemResponseCode.getCode())
                return populateResponseJson(code, systemResponseCode.getMsg());
        }
        return populateResponseJson();
    }

    /**
     * 组装json响应格式-错误3
     *
     * @param code
     * @param message
     * @return
     */
    public static String populateResponseJson(int code, String message) {
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        Map<String, Object> stateMap = new HashMap<String, Object>();
        stateMap.put("code", code);
        stateMap.put("msg", message);
        jsonMap.put("state", stateMap);
        return JsonUtils.obj2String(jsonMap);
    }
}
